package com.wzcl.app.controller.student;

import com.wzcl.app.model.common.BusinessException;
import com.wzcl.app.model.common.CodeMsg;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.WrongTopicDto;
import com.wzcl.app.model.vo.WrongTopicPreviewVo;
import com.wzcl.app.model.vo.WrongTopicVo;
import com.wzcl.app.service.WrongTopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Log4j2
@RestController
@RequestMapping("/userWrongTopic")
@Api(value = "用户错题管理controller", tags = {"用户错题管理接口"})
public class UserWrongTopicController {

    @Autowired
    private WrongTopicService wrongTopicService;

    @ApiOperation("添加错题")
    @PostMapping("/add")
    private RVO<String> addWrongTopic(@RequestBody @Valid WrongTopicDto wrongTopicDto){
        wrongTopicDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        wrongTopicService.addWrongTopic(wrongTopicDto);
        return RVO.success();
    }

    @ApiOperation("移除错题")
    @PostMapping("/remove")
    private RVO<String> removeWrongTopic(@RequestBody WrongTopicDto wrongTopicDto){
        if(wrongTopicDto == null || wrongTopicDto.getTopicId() == null){
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL,"缺少题目Id");
        }
        wrongTopicDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        wrongTopicService.delWrongTopicByTopicIdAndUserPhone(wrongTopicDto.getTopicId(),wrongTopicDto.getUserPhone());
        return RVO.success();
    }

    @ApiOperation("错题题目总数")
    @PostMapping("/countTopic")
    private RVO<Long> countTopic(@RequestBody WrongTopicDto wrongTopicDto){
        wrongTopicDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        Long total = wrongTopicService.countWrongTopicGroupByTopic(wrongTopicDto);
        return RVO.success(total);
    }

    @ApiOperation("错题知识点总数")
    @PostMapping("/countKnowledgePoint")
    private RVO<Long> countKnowledgePoint(@RequestBody WrongTopicDto wrongTopicDto){
        wrongTopicDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        Long total = wrongTopicService.countWrongTopicGroupByKnowledgePoint(wrongTopicDto);
        return RVO.success(total);
    }

    @ApiOperation("错题知识点分类")
    @PostMapping("/findCondition")
    private RVO<List<WrongTopicPreviewVo>> findCondition(@RequestBody WrongTopicDto wrongTopicDto){
        wrongTopicDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        return RVO.success(wrongTopicService.findCondition(wrongTopicDto));
    }

    @ApiOperation("分页查错题")
    @PostMapping("/findWrongTopicPage")
    private RVO<List<WrongTopicVo>> findWrongTopicPage(@RequestBody WrongTopicDto wrongTopicDto){
        wrongTopicDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        MyPage<WrongTopicVo> page = wrongTopicService.findWrongTopicPage(wrongTopicDto);
        if(page != null){
            return RVO.success(page.getContent(),page);
        }
        return RVO.success(null);
    }
}
